<%@page language="java" contentType="text/html; charset=utf-8"%>
<%@include file="/commons/inc.jsp" %>

<h3 id="navTitleId">药物调查 <input type="button" onclick="saveEdit('save');" value="保存"></h3>

<script>
setTimeout("optionAdd()",1000);
setTimeout("display()",1000);

function optionAdd(id){
	var flag = false; 
	var s = $('s'+id);
	<s:iterator value="drugs" id="opind" status="st">
		var fatherid = '<s:property value='#opind.fatherid'/>';
	  	if(id==fatherid){
	  	    var drugname = '<s:property value='#opind.drugname'/>';
		    var drugid = '<s:property value='#opind.drugid'/>';
			var option = document.createElement('option');
	        s.options.add(new Option(drugname,drugid));
	        flag = true;
	  	}
	</s:iterator>
	if(s){
		s.onchange = function(){
			var t = s.options[s.selectedIndex].text;
			if(t=="其他"){
				$('div'+id).style.display = "";
			}else{
				$('div'+id).style.display = "none";
			}
		}
	}
	if(!flag){
		var s = $('s'+id);
		if(s){
			s.parentNode.removeChild(s);
		}
	}
}

function display(){
	var drugtype = '<s:property value='drugSurvey.drugtype'/>';
	var drugname = '<s:property value='drugSurvey.drugname'/>';
	var drugamount ='<s:property value='drugSurvey.amount'/>';
	var drugunit = '<s:property value='drugSurvey.takeunit'/>';
	var drugmethod = '<s:property value='drugSurvey.takemethod'/>';
	var drugfreq = '<s:property value='drugSurvey.takefreq'/>';
	var other = '<s:property value='drugSurvey.othername'/>';
	
	if(drugtype && drugname && drugamount && drugunit && drugmethod && drugfreq){
		var k = 0;
		drugtype = drugtype.split(",");
		drugname = drugname.split(",");
		drugamount = drugamount.split(",");
		drugunit = drugunit.split(",");
		drugmethod = drugmethod.split(",");
		drugfreq = drugfreq.split(",");
		if(other && other!=""){
			other = other.split(",");
		}
		for(var i=0;i<drugtype.length;i++){
			$('id'+ drugtype[i].trim()).checked = true;
			var s = $('s'+ drugtype[i].trim());
			if(s){
				for(var j=0;j<s.options.length;j++){
					if(drugname[i] == s.options[j].value){
						s.options[j].selected = true;
						if(s.options[j].text=="其他"){
							$('div'+ drugtype[i].trim()).style.display = "";
							if(other[k]){
								$('other'+ drugtype[i].trim()).value = other[k++];	
							}	
						}
					}
				}
			} 
			
			var u = $('u'+ drugtype[i].trim());
			if(u){
				for(var j=0;j<u.options.length;j++){
					if(drugunit[i] == u.options[j].value){
						u.options[j].selected = true;
					}
				}
			} 

			var m = $('m'+ drugtype[i].trim());
			if(m){
				for(var j=0;j<m.options.length;j++){
					if(drugmethod[i] == m.options[j].value){
						m.options[j].selected = true;
					}
				}
			} 

			var f = $('f'+ drugtype[i].trim());
			if(f){
				for(var j=0;j<f.options.length;j++){
					if(drugfreq[i] == f.options[j].value){
						f.options[j].selected = true;
					}
				}
			} 
			if($('a'+ drugtype[i].trim())){
				$('a'+ drugtype[i].trim()).value = drugamount[i].trim();
			}
		}
	}	 	
}

function showtips(tips,flag){ 
	var tip=document.getElementById(tips);
	if(tip.style.display=="none"){ 
	        tip.style.display="inline";
	        //my_tips.style.left=event.clientX+10; 
	        //my_tips.style.top=event.clientY+10; 
	}else{ 
	      tip.style.display="none"; 
	} 
}

</script>

<body id="body">

<%@include file="/commons/showmessage.jsp" %>

<form name="drugform" id="drugform" method="post" action="${contextPath}/drug/drug.action?method=saveEditDrugSurvey">
	<s:hidden name="drugSurvey.seqid" id="drugSurvey.seqid"></s:hidden>
	<s:hidden name="drugSurvey.patientid" id="drugSurvey.patientid"></s:hidden>
	<s:hidden name="patientid" id="patientid"></s:hidden>
	<s:hidden name="drugtype" id="drugtype"></s:hidden>
	<s:hidden name="drugname" id="drugname"></s:hidden>
	<s:hidden name="drugamount" id="drugamount"></s:hidden>
	<s:hidden name="drugunit" id="drugunit"></s:hidden>
	<s:hidden name="drugmethod" id="drugmethod"></s:hidden>
	<s:hidden name="drugfreq" id="drugfreq"></s:hidden>
	<s:hidden name="othername" id="othername"></s:hidden>
	
	<table border="0" cellspacing="1" cellpadding="0" class="formTable" width="100%">
		 <thead>
			<tr>
				<td width="32%" align="center" class="form-th">类型</td>
				<td width="20%" align="center" class="form-th">药名</td>
				<td width="12%" align="center" class="form-th">单次剂量</td>	
				<td width="12%" align="center" class="form-th">单位</td>	
				<td width="12%" align="center" class="form-th">用法</td>	
				<td width="12%" align="center" class="form-th">频次</td>	
			</tr>
	   </thead>
		 
		 <s:iterator value="drugs" id="ind" status="st">
			 <s:if test="fatherid==0">
			  <s:set name="parentId" value="#ind.drugid"/>
				<tbody>			
					 <tr>
						<td align="left" class="form-td">
							<input type="checkbox" id="id${drugid}" name="drugSurvey.drugtype" onclick="javascript:sel(this,${fatherid});" value="${drugid}"/><b><font style="vertical-align:bottom;">${drugname}</font></b>
							<s:if test="drugname=='胰岛素和胰岛素类似物'">
							<br><a onclick="showtips('14-1',1);showtips('14-2',1);">【说明】
							<br><img id='14-1' style="display:none" src="../images/tips/14-1.png" />
							<br><img id='14-2' style="display:none" src="../images/tips/14-2.png" />
							</a>
							</s:if>
							</td>			
							<s:if test="drugid==5">
								 <td align="left" class="form-td">
									<select name="s${drugid}" id="s${drugid}" style="width:220px;">
											<script>optionAdd(${drugid})</script>
									</select><br>
									<div id="div${drugid}" style="display:none">
										<input type="text" name="drugSurvey.othername" id="other${drugid}" />
									</div></td>				 
								 <td align="left" class="form-td">
									<input type="text" size="10" name="drugSurvey.amount" id="a${drugid}"/></td>
								 <td align="left" class="form-td">
									<select name="drugSurvey.takeunit" id="u${drugid}">
										<option value="0">请选择</option>
										<option value="1">mg</option>
										<option value="2">μg</option>
										<option value="3">U</option>
										<option value="4">g</option>
									</select></td>
								<td align="left" class="form-td">
									<select name="drugSurvey.takemethod" id="m${drugid}">
										<option value="0">请选择</option>
										<option value="1">口服</option>
										<option value="2">水煎服</option>
										<option value="3">皮下注射</option>
										<option value="4">静推</option>
										<option value="5">静滴</option>
									</select></td>
								<td align="left" class="form-td">
									<select name="drugSurvey.takefreq" id="f${drugid}">
										<option value="0">请选择</option>
										<option value="1">1/日</option>
										<option value="2">2/日</option>
										<option value="3">3/日</option>
										<option value="4">4/日</option>
										<option value="5">早餐</option>
										<option value="6">中餐</option>
										<option value="7">晚餐</option>
										<option value="8">睡前</option>
										<option value="9">10pm</option>
										<option value="10">其他</option>
									</select></td>	
							</s:if>
							<s:elseif test="drugid==6">
							 <td align="left" class="form-td" colspan="5">
							 	<textarea style="height:50px;width:660px;" name="drugSurvey.chndrug" id="drugSurvey.chndrug">${drugSurvey.chndrug}</textarea>
							 </td>
							</s:elseif>
							<s:else><td align="left" class="form-td" colspan="5"></td></s:else>
					</tr>	
				</tbody>
			 </s:if>
			 
			 <s:iterator value="drugs" id="in" status="index">
			  <s:if test="#parentId!=5 && #parentId!=6 && #parentId==fatherid">
			  	<tbody id='tbody<s:property value="#st.getIndex()"/>'>
					<tr>
						<td align="left" class="form-td">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<input type="checkbox" id="id${drugid}" name="drugSurvey.drugtype" onclick="javascript:sel(this,${fatherid});" value="${drugid}"/><font style="vertical-align:bottom;">${drugname}</font></td>
						<td align="left" class="form-td">
							<select name="s${drugid}" id="s${drugid}" style="width:220px;">
								<script>optionAdd(${drugid})</script>
							</select>
							<div id="div${drugid}" style="display:none">
								<input type="text" name="drugSurvey.othername" id="other${drugid}" />
							</div></td>
						<td align="left" class="form-td">
							<input type="text" size="10" name="drugSurvey.amount" id="a${drugid}"/></td>
						<td align="left" class="form-td">
							<select name="drugSurvey.takeunit" id="u${drugid}">
								<option value="0">请选择</option>
								<option value="1">mg</option>
								<option value="2">μg</option>
								<option value="3">U</option>
								<option value="4">g</option>
							</select></td>
						<td align="left" class="form-td">
							<select name="drugSurvey.takemethod" id="m${drugid}">
								<option value="0">请选择</option>
								<option value="1">口服</option>
								<option value="2">水煎服</option>
								<option value="3">皮下注射</option>
								<option value="4">静推</option>
								<option value="5">静滴</option>
							</select></td>
						<td align="left" class="form-td">
							<select name="drugSurvey.takefreq" id="f${drugid}">
								<option value="0">请选择</option>
								<option value="1">1/日</option>
								<option value="2">2/日</option>
								<option value="3">3/日</option>
								<option value="4">4/日</option>
								<option value="5">早餐</option>
								<option value="6">中餐</option>
								<option value="7">晚餐</option>
								<option value="8">睡前</option>
								<option value="9">10pm</option>
								<option value="10">其他</option>
							</select></td>
					</tr>	
				</tbody>	 	
			  </s:if>
			</s:iterator>
		 </s:iterator>
		  <script>display()</script>
	</table>
	<table>
		<tr>
			<td align="center">
			<input type="reset" value="重置">
		    <input type="button" onclick="saveEdit('save');" value="保存"></td>
		</tr>
	</table>
</form>

<%@include file="/commons/tableSel.jsp" %>

</body>

<style type="text/css">
body {
	height: 100%;
	width: 100%;
}
</style>

<script>
function saveEdit(type){
	if(type=='refresh'){
		location.reload();
	}else if(type=='save'){
		if(!Validator.Validate($('drugform'),3)){
			return;
		}
		
		var s = document.getElementsByName('drugSurvey.drugtype');
		var ids = "";
		var name = "";
		var amount = "";
		var unit = "";
		var method = "";
		var freq = "";
		var other = "";
		
		for(var i=0;i<s.length;i++){
			if(s[i].checked){
				var id = s[i].value;
				ids += id + ",";
				
				var d = $('s'+id);
				if(d){
					name += d.options[d.selectedIndex].value + ",";
				}else{
					name += "0,";
				}

				var u = $('u'+id);
				if(u){
					unit += u.options[u.selectedIndex].value + ",";
				}else{
					unit += "0,";
				}

				var m = $('m'+id);
				if(m){
					method += m.options[m.selectedIndex].value + ",";
				}else{
					method += "0,";
				}

				var f = $('f'+id);
				if(f){
					freq += f.options[f.selectedIndex].value + ",";
				}else{
					freq += "0,";
				}
				
				var a = $('a'+id);
				if(a && a.value && a.value!=""){
					if(a.value.indexOf(",")==-1){
						amount += a.value + ","; 
					}else{
						alert("输入内容有误，请更正!");
						return;
					}		
				}else{
					amount += "0,";
				}

				var divother = $('div'+ id);
				if(divother &&　divother.style.display=="" && $('other'+ id)){
					if($('other'+ id).value.indexOf(",")==-1){
						other += $('other'+ id).value + ",";
					}else{
						alert("输入内容有误，请更正!");
						return;
					}				
				}
			}
		}
		if(ids!=""){
			ids = ids.substring(0,ids.length-1);
		}
		if(name!=""){
			name = name.substring(0,name.length-1);
		}
		if(amount!=""){
			amount = amount.substring(0,amount.length-1);
		}
		if(unit!=""){
			unit = unit.substring(0,unit.length-1);
		}
		if(method!=""){
			method = method.substring(0,method.length-1);
		}
		if(freq!=""){
			freq = freq.substring(0,freq.length-1);
		}
		if(other!=""){
			other = other.substring(0,other.length-1);
		}
		
		$('drugform').drugtype.value = ids;
		$('drugform').drugname.value = name;
		$('drugform').drugamount.value = amount;
		$('drugform').drugunit.value = unit;
		$('drugform').drugmethod.value = method;
		$('drugform').drugfreq.value = freq;
		$('drugform').othername.value = other;
		$('drugform').submit();
	}
}
function sel(obj,fatherid){
	if(fatherid==0){
		<s:iterator value="drugs" id="selind" status="st">
			var fid = '<s:property value='#selind.fatherid'/>';
			var drugid = '<s:property value='#selind.drugid'/>';
		  	if(fid && fid!="" && obj.value == fid){
			  	if($('id'+drugid)){
			  		$('id'+drugid).checked = obj.checked;
			  	}	  		
		  	}
		</s:iterator>
	}else{
		var f = $('id'+fatherid);
		if(f){
			if(obj.checked){	
				if(!f.checked){
					 f.checked = true;
				}
			}else{
				var flag = false;
				<s:iterator value="drugs" id="selind" status="st">
					var fid = '<s:property value='#selind.fatherid'/>';
					var drugid = '<s:property value='#selind.drugid'/>';
				  	if(fatherid == fid){
				  		if($('id'+drugid).checked){
					  		flag = true;
				  		}
				  	}
				</s:iterator>
				if(!flag && f.checked){
					 f.checked = false;
				}
			}
		}
	}
}
</script>